<!DOCTYPE html>
<html>
<head>
<div th:include="base-libs :: base-libs" />
<title>选择医保服务项</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">

<style>

</style>
<script th:inline="javascript">
    var JGXZQHDM = [[${JGXZQHDM}]];
</script>
<script>
    var managerData = StoreCache.removeCache("managerData");
    var total = 0;
	$(function(){
    	initView();
    })

    function initView() {
		$("#centerTb").datagrid({
			striped : true,
			clickToSelect: true,
			fit:true,
			fitColumns:true,
			singleSelect:true,
			columns : [[
					{field : "FWID",title : "服务ID",width:100,align:'center'},
					{field : "FWMC",title : "服务名称",width:100,align:'center'},
					{field : "SFBZ",title : "单价(元)",width:80,align:'center'},
					{field : "FWCS",title : "服务次数",width:100,align:'center',formatter:function(value, row){
							if(managerData.YBFWX_ARR){
								var YBFWX_ARR = JSON.parse(managerData.YBFWX_ARR);
								$.each(YBFWX_ARR, function(){
									if(this.FWID == row.FWID){
										row.FWCS = this.FWCS;
										row.SFBZ = this.DJ;
										row.ZJ = this.ZJ;
                                        total += parseFloat(this.ZJ);
										return false;
									}
								})
							}
							return row.FWCS || 0;
						},
						editor:{
							type: "numberbox",
						}
					},
					{field : "ZJ",title : "总价(元)",width:100,align:'center',formatter:function(value, row){
							row.ZJ = value || "0.00";
                        	return row.ZJ;
						},
						editor:{
							type: "text",
						}
					}
			]],
			url : contextRoot+"fwxx/queryAll.service",
			onBeforeLoad : function(params) {
				params["FWLB"] = "04";
				params["FWZT"] = 1;
				//params["XZQHDM"] = JGXZQHDM;
				return params;
			},
			onClickRow : function(rowIndex, row){
				var editRowNum = $(this).data("editRowNum");
				if(!StringUtils.isEmpty(editRowNum) && rowIndex != editRowNum){
					$(this).datagrid('endEdit', editRowNum);
				}
				$(this).data("editRowNum",rowIndex).datagrid('beginEdit', rowIndex);
				$(".textbox-text:first").data("SFBZ",row.SFBZ).css("text-align","center").focus();

				//total_ : 除了当前行之外的所有行的总计（这样在算总和时只需要：total_ + 当前行total）
				var total_ = parseFloat($("#total").text())-parseFloat($(".datagrid-editable-input:last").val());
				$("#total").data("total_",total_);

				//伪装总价input,达到隐藏效果
				$(".datagrid-editable-input:last").css({"background":"#FFE4A5","border":"0","color":"#000","text-align":"center"})
					.attr("readonly","readonly");
			},
			onLoadSuccess:function(){
                $("#total").text(total);
                delete managerData.YBFWX_ARR;
			}
		});
		//计算单项服务的总价
		$("body").on("keyup",".textbox-text:first",function(){
		    var fwcs = $(this).val(),
                SFBZ = $(this).data("SFBZ");
		    var zj = (SFBZ*fwcs).toFixed(2);
            $(".datagrid-editable-input:last").val(zj);

            var total = (parseFloat(zj) + parseFloat($("#total").data("total_"))).toFixed(2);
            $("#total").text(total);
            if(managerData.YBJJZF_MAX<total){
				$.QuickAlert.alertFail({content: "哎呀，总价超出范围了！"});
			}
		})

		/*取消(关闭)*/
		$("#btnQx").click(function(){
		    var index = parent.layer.getFrameIndex(window.name);
		    parent.layer.close(index);
		})
		/*确认*/
		$("#btnQr").click(function(){
			if($("#total").text()==0){
			    $.QuickAlert.alertFail({content: "请至少选择一项医保服务，并指定您需要的服务次数"});
			}else if(managerData.YBJJZF_MAX < parseFloat($("#total").text())){
                $.QuickAlert.alertFail({content: "哎呀，总价超出范围了！"});
            }else{
			    //step 1 : 关闭编辑模式
                var editRowNum = $("#centerTb").data("editRowNum");
                if(!StringUtils.isEmpty(editRowNum)){
                    $("#centerTb").datagrid('endEdit', editRowNum);
                }
                //step 2 : 获取数据
				var data = $("#centerTb").datagrid("getRows");
                var YBFWX_ARR = [],total = 0;
                $.each(data, function(){
					if(this.FWCS>0){
                        YBFWX_ARR.push({
                            FWID: this.FWID,
							FWMC:this.FWMC,
							DJ: this.SFBZ,
							FWCS: this.FWCS,
							ZJ: this.ZJ
						})
					}
				})

				StoreCache.setCache("YBFWX_ARR",JSON.stringify(YBFWX_ARR));
				$("#btnQx").trigger("click");
			}
		})

	}


	function centerTbLoadSuccess(){

	}
    	
    	
   	
   
</script>

</head>

<body class="easyui-layout" border="false">
	<div region="center" border="false">
		<table id="centerTb" border="false"></table>
	</div>
	<div region="south" style="height:75px; border:none; overflow:hidden;background-color:#f9f9f9;padding-top:6px; text-align:center;">
		<div style="height:25px;line-height:25px;text-align:right;padding-right:70px;font-size:14px;font-weight:bold;border-bottom:1px solid #ccc">
			<!--<span>总价范围：</span>
			<span style="font-size:16px;color:blue">0-</span>
			<span id="total_">100</span>
			<span>元</span>-->
			<span style="margin-left:35px;">当前总价：</span>
			<span id="total" style="font-size:16px;color:blue">0</span>
			<span>元</span>
		</div>
		<div style="height:40px;line-height:40px;">
			<button id="btnQr" type="button" style=" margin-left: 10px;">确认</button>
			<button id="btnQx" type="button" style=" margin-left: 10px;">取消</button>
		</div>
	</div>
</body>
</html>
